package com.ext.aic.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.recyclerview.widget.RecyclerView;

import com.ext.aic.R;
import com.ext.aic.beans.ProductItem;
import com.ext.aic.databinding.ReferenceStyleItemBinding;
import com.ext.aic.holder.ReferenceStyleViewHolder;
import com.eyinfo.android_pure_utils.utils.ConvertUtils;
import com.eyinfo.android_pure_utils.utils.ObjectJudge;

import java.util.ArrayList;
import java.util.List;

public class ReferenceStyleListAdapter extends RecyclerView.Adapter<ReferenceStyleViewHolder> {

    private List<ProductItem> items = new ArrayList<>();

    public void addItems(List<ProductItem> items) {
        this.items.addAll(items);
    }

    public void addItem(ProductItem item) {
        this.items.add(item);
    }

    @Override
    public ReferenceStyleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.reference_style_item, parent, false);
        return new ReferenceStyleViewHolder(ReferenceStyleItemBinding.bind(view));
    }

    @Override
    public void onBindViewHolder(ReferenceStyleViewHolder holder, int position) {
        if (ObjectJudge.isNullOrEmpty(items)) {
            holder.referenceStyleItemRl.setVisibility(View.GONE);
            holder.referenceStyleAddRl.setVisibility(View.VISIBLE);
        } else {
            holder.referenceStyleItemRl.setVisibility(View.VISIBLE);
            holder.referenceStyleAddRl.setVisibility(View.GONE);
            holder.closeIv.setTag(position);
            holder.closeIv.setOnClickListener(this::onDeleteClick);
        }
    }

    public void onDeleteClick(View view) {
        int position = ConvertUtils.toInt(view.getTag());
        items.remove(position);
        notifyItemRemoved(position);
    }

    @Override
    public int getItemCount() {
        return 1;
    }
}
